package com.gitlab.lucky.weixin.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gitlab.lucky.common.dao.mapper.NoticeMapper;
import com.gitlab.lucky.common.dao.model.Notice;
import com.gitlab.lucky.common.enums.Constant;
import com.gitlab.lucky.common.service.ServiceImpl;
import com.gitlab.lucky.common.redis.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @program: lucky
 * @author: Mr.Han
 * @create: 2019-03-21 16:19
 **/
@Service
public class NoticeService extends ServiceImpl<NoticeMapper, Notice> {
    @Autowired
    private RedisService redisService;


    /**
     * 查询个人中心公告
     * @param platform
     * @return
     */
    public List<Notice> selectNoticeList(Integer platform) {
        Object noticeJSON = redisService.get(Constant.NOTICE_KEY + platform);
        if (noticeJSON == null) {
            Example example = new Example(Notice.class);
            example.createCriteria().andEqualTo("platform", platform).andEqualTo("isDelete",Constant.FALSE);
            example.orderBy("createTime").desc();
            List<Notice> notices = this.selectByExample(example);
            redisService.set(Constant.NOTICE_KEY + platform, JSONObject.toJSONString(notices));
            return notices;
        }
        List<Notice> notices = JSONArray.parseArray(noticeJSON.toString(), Notice.class);
        return notices;
    }
}
